package com.clps.cn.Activiteday04;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 启动流程实例，动态设置assignee
 */
public class AssigneeUEL {

    @Test
    public  void  test01Instance(){
        //01创建ProcressEngine对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

        //02得到RepositoryService实例
        RepositoryService repositoryService = processEngine.getRepositoryService();

        //03：进行部署
        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("diagram/holiday2.bpmn")
                .addClasspathResource("diagram/holiday2.png")
                .name("填写请假单")
                .deploy();


        //输出部署到某一个位置
        System.out.println("deployment.getName() = " + deployment.getName());
        System.out.println("deployment.getId() = " + deployment.getId());
    }


    public static void main(String[] args) {

        //01得到ProcessEngine对象
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();

        //得到runtimeService对象
        RuntimeService runtimeService = defaultProcessEngine.getRuntimeService();

        //03:设置assignee的取值，用户可以在界面上设置流程的执行人
        Map<String, Object> map = new HashMap<>();
        map.put("assigneeO","zhangsan");
        map.put("assignee1","lisi");
        map.put("assignee2","wangwu");

        //04启动流程实例，同时还要设置assignee的值
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("holiday", map);

        System.out.println("processInstance.getName() = " + processInstance.getName());

    }

    //张三执行任务
    @Test
    public  void  test001zhansgan(){
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();

        TaskService taskService = defaultProcessEngine.getTaskService();

        //03：根据流程定义的key得到任务表
        List<Task> list = taskService.createTaskQuery()
                .processDefinitionKey("holiday")
                .taskAssignee("zhangsan")
                .list();
        //任务列表
        for (Task task : list) {
            System.out.println("task.getProcessInstanceId() = " + task.getProcessInstanceId());
            System.out.println("task.getId() = " + task.getId());
            System.out.println("task.getAssignee() = " + task.getAssignee());
            System.out.println("task.getName() = " + task.getName());
            taskService.complete(task.getId());
        }
    }
}
